package online.lmds.sdmanage.common.entity;

import online.lmds.sdmanage.common.Constants;
import online.lmds.sdmanage.common.Status;

import java.util.HashMap;

/**
 * Created by Brighten Ma on 2017/4/11.
 * 接口Json 返回
 */
public class Result extends HashMap<String, Object> {


    /**接口版本 */
    private static final String VERSION = Constants.API_VERSION;

    /** 成功 */
    public static final Integer CODE_SUCCESS = 1;

    /** 失败 */
    public static final Integer CODE_ERROR = 2;


    public Result() {
        put("version", VERSION);
    }

    /** 设置结果代码 */
    public Result putCode(Integer code){
        put("code", code);
        return this;
    }

    /** 设置结果成功*/
    public Result setSuccessCode() {
        put("code", CODE_SUCCESS);
        setStatus(Status.SUCCEED);
        return this;
    }

    /** 设置结果失败*/
    public Result setErrorCode() {
        put("code", CODE_ERROR);
        return this;
    }

    /** 返回结果代码 */
    public Integer getResultCode() {
        return (Integer) get("code");
    }

    public boolean isSuccess() {
        Integer code = getResultCode();
        return code != null && code.equals(CODE_SUCCESS);
    }

    public boolean isError() {
        Integer code = getResultCode();
        return code != null && code.equals(CODE_ERROR);
    }

    /** 设置提示信息 */
    public Result setMessage(String message) {
        put("message", message);
        return this;
    }

    public Result setStatus(Status status){
        put("status", status.value);
        put("statusMsg", status.msg);

        if (get("message") != null){
            put("message", status.msg);
        }
        return this;
    }




    /** 设置数据 */
    public Result setData(Object data) {
        put("data", data);
        return this;
    }

    public Object getData(){
        return get("data");
    }

}
